using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using BaseAmd.Mmp;
using BaseAmd.Mmp.FileNameFormat;
using Newtonsoft.Json;
using Catastrophe.Common.FileM;
using Catastrophe.Common.Http;

namespace SiemensAmd.Mmp.Http.Net
{
	public class ProgHttpHandlerNet : IProgHandler
	{
		private string _httpServerIp = "localhost";

		private int _httpServerPort = 8011;

		public const int DefaultRequestTimeout = 10000;

		public Action<int, int, FileInfoEntity> DownLoadNcProcessHandler { get; set; }

		public Action<int, int> UploadPcProcessHandler { get; set; }

		public ProgHttpHandlerNet(ProgApiResources progApiRes)
		{
			HttpApiResources httpApiResources = (HttpApiResources)progApiRes;
			this._httpServerIp = httpApiResources.IpAdr;
		}

		public TransResult DownLoadNcSingle(FileInfoEntity srcFile, FileInfoEntity dstFileInfo, ProgApiResources apiRes)
		{
			return this.DownLoadNcMulti(new List<FileInfoEntity> { srcFile }, dstFileInfo.Path, apiRes);
		}

		public TransResult DownLoadNcMulti(List<FileInfoEntity> entityBuf, string remotePath, ProgApiResources apiRes)
		{
			try
			{
				HttpApiResources res = (HttpApiResources)apiRes;
				DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(37, 2);
				defaultInterpolatedStringHandler.AppendLiteral("http://");
				defaultInterpolatedStringHandler.AppendFormatted(this._httpServerIp);
				defaultInterpolatedStringHandler.AppendLiteral(":");
				defaultInterpolatedStringHandler.AppendFormatted(this._httpServerPort);
				defaultInterpolatedStringHandler.AppendLiteral("/amd/ProgM/?function=DownLoad");
				string url = defaultInterpolatedStringHandler.ToStringAndClear();
				List<ProgContent> list = new List<ProgContent>();
				foreach (FileInfoEntity item in entityBuf)
				{
					list.Add(new ProgContent
					{
						ProgName = item.FormatName,
						Path = remotePath,
						Content = item.ContentStr
					});
				}
				string value = HttpUtil.HttpPost(url, JsonConvert.SerializeObject(list), "application/json", this.GetTimeout(res));
				ExcuteResult excuteResult = JsonConvert.DeserializeObject<ExcuteResult>(value);
				return new TransResult
				{
					IsSuccess = (excuteResult.Result == RequestResultEnum.Ok),
					Message = excuteResult.Message
				};
			}
			catch (Exception ex)
			{
				return new TransResult
				{
					Message = ex.ToString()
				};
			}
		}

		public TransResult UploadPcSingle(FileInfoEntity remoteFileInfo, string localPath, ProgApiResources apiRes)
		{
			try
			{
				HttpApiResources res = (HttpApiResources)apiRes;
				List<ProgContent> value = new List<ProgContent>
				{
					new ProgContent
					{
						ProgName = remoteFileInfo.Name,
						Path = remoteFileInfo.Path
					}
				};
				DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(40, 2);
				defaultInterpolatedStringHandler.AppendLiteral("http://");
				defaultInterpolatedStringHandler.AppendFormatted(this._httpServerIp);
				defaultInterpolatedStringHandler.AppendLiteral(":");
				defaultInterpolatedStringHandler.AppendFormatted(this._httpServerPort);
				defaultInterpolatedStringHandler.AppendLiteral("/amd/ProgM/?function=UpLoadMulti");
				string url = defaultInterpolatedStringHandler.ToStringAndClear();
				string value2 = HttpUtil.HttpPost(url, JsonConvert.SerializeObject(value), "application/json", this.GetTimeout(res));
				ExcuteResult excuteResult = JsonConvert.DeserializeObject<ExcuteResult>(value2);
				if (excuteResult.Result == RequestResultEnum.Ok)
				{
					List<ProgContent> source = JsonConvert.DeserializeObject<List<ProgContent>>(excuteResult.JsonValue);
					string path = Path.Combine(localPath, remoteFileInfo.Name);
					using (StreamWriter streamWriter = new StreamWriter(path))
					{
						streamWriter.Write(source.FirstOrDefault()?.Content);
					}
					return new TransResult
					{
						IsSuccess = true,
						Content = source.FirstOrDefault()?.Content.Trim()
					};
				}
				return new TransResult
				{
					Message = excuteResult.Message
				};
			}
			catch (Exception ex)
			{
				return new TransResult
				{
					Message = ex.ToString()
				};
			}
		}

		public TransResult Rename(string srcName, string destName, string root, ProgApiResources apiRes)
		{
			try
			{
				HttpApiResources res = (HttpApiResources)apiRes;
				FileInfo fileInfo = new FileInfo(destName);
				DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(54, 4);
				defaultInterpolatedStringHandler.AppendLiteral("http://");
				defaultInterpolatedStringHandler.AppendFormatted(this._httpServerIp);
				defaultInterpolatedStringHandler.AppendLiteral(":");
				defaultInterpolatedStringHandler.AppendFormatted(this._httpServerPort);
				defaultInterpolatedStringHandler.AppendLiteral("/amd/ProgM/?function=Rename&fullName=");
				defaultInterpolatedStringHandler.AppendFormatted(srcName);
				defaultInterpolatedStringHandler.AppendLiteral("&dstName=");
				defaultInterpolatedStringHandler.AppendFormatted(fileInfo.Name);
				string url = defaultInterpolatedStringHandler.ToStringAndClear();
				string value = HttpUtil.HttpPost(url, "", "application/json", this.GetTimeout(res));
				ExcuteResult excuteResult = JsonConvert.DeserializeObject<ExcuteResult>(value);
				return new TransResult
				{
					IsSuccess = (excuteResult.Result == RequestResultEnum.Ok),
					Message = excuteResult.Message
				};
			}
			catch (Exception ex)
			{
				return new TransResult
				{
					Message = ex.ToString()
				};
			}
		}

		public TransResult DeleteFile(List<string> fileAry, ProgApiResources apiRes)
		{
			try
			{
				DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(39, 2);
				defaultInterpolatedStringHandler.AppendLiteral("http://");
				defaultInterpolatedStringHandler.AppendFormatted(this._httpServerIp);
				defaultInterpolatedStringHandler.AppendLiteral(":");
				defaultInterpolatedStringHandler.AppendFormatted(this._httpServerPort);
				defaultInterpolatedStringHandler.AppendLiteral("/amd/ProgM/?function=DeleteFile");
				string url = defaultInterpolatedStringHandler.ToStringAndClear();
				string value = HttpUtil.HttpPost(url, JsonConvert.SerializeObject(fileAry), "application/json", 200000);
				ExcuteResult excuteResult = JsonConvert.DeserializeObject<ExcuteResult>(value);
				return new TransResult
				{
					IsSuccess = (excuteResult.Result == RequestResultEnum.Ok),
					Message = excuteResult.Message
				};
			}
			catch (Exception value2)
			{
				Console.WriteLine(value2);
				throw;
			}
		}

		public Tuple<TransResult, List<FileInfoEntity>> GetProgDetails(string stationName, ProgApiResources apiRes)
		{
			try
			{
				HttpApiResources httpApiResources = (HttpApiResources)apiRes;
				DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(49, 3);
				defaultInterpolatedStringHandler.AppendLiteral("http://");
				defaultInterpolatedStringHandler.AppendFormatted(this._httpServerIp);
				defaultInterpolatedStringHandler.AppendLiteral(":");
				defaultInterpolatedStringHandler.AppendFormatted(this._httpServerPort);
				defaultInterpolatedStringHandler.AppendLiteral("/amd/ProgM/?function=GetProgDetails&path=");
				defaultInterpolatedStringHandler.AppendFormatted(httpApiResources.RemotePath);
				string url = defaultInterpolatedStringHandler.ToStringAndClear();
				string value = HttpUtil.HttpPost(url, "", "application/json", this.GetTimeout(httpApiResources));
				ExcuteResult excuteResult = JsonConvert.DeserializeObject<ExcuteResult>(value);
				List<FileInfoEntity> list = new List<FileInfoEntity>();
				if (excuteResult.Result == RequestResultEnum.Ok)
				{
					List<HttpFilePropertyEntity> list2 = JsonConvert.DeserializeObject<List<HttpFilePropertyEntity>>(excuteResult.JsonValue);
					int queryParentNodeId = 1;
					for (int i = 0; i < list2.Count; i++)
					{
						FileInfoEntity fileInfoEntity = new FileInfoEntity
						{
							QueryNodeId = i + 1,
							Name = list2[i].SrcName,
							FormatName = list2[i].FormatName,
							Path = list2[i].Path,
							FullName = list2[i].FullName,
							Extension = list2[i].Extension,
							Size = list2[i].Size,
							IsDirectory = list2[i].IsDirectory,
							ModifyTime = list2[i].AccessTime,
							IsExist = true,
							IsValid = true,
							Type = (list2[i].IsDirectory ? FilePathTypeEnum.Dir : FilePathTypeEnum.File)
						};
						if (list2[i].Extension != "")
						{
							fileInfoEntity.NameWithoutExtension = fileInfoEntity.Name.Replace(list2[i].Extension, "");
						}
						if (fileInfoEntity.Extension.StartsWith("."))
						{
							fileInfoEntity.ExtensionWithoutPoint = fileInfoEntity.Extension.Remove(0, 1);
						}
						if (list2[i].IsDirectory)
						{
							queryParentNodeId = i + 1;
							fileInfoEntity.QueryParentNodeId = -1;
						}
						else
						{
							fileInfoEntity.QueryParentNodeId = queryParentNodeId;
						}
						list.Add(fileInfoEntity);
					}
					return new Tuple<TransResult, List<FileInfoEntity>>(new TransResult
					{
						IsSuccess = true
					}, list);
				}
				return new Tuple<TransResult, List<FileInfoEntity>>(new TransResult
				{
					IsSuccess = false,
					Message = excuteResult.Message
				}, list);
			}
			catch (Exception ex)
			{
				return new Tuple<TransResult, List<FileInfoEntity>>(new TransResult
				{
					IsSuccess = false,
					Message = ex.ToString()
				}, new List<FileInfoEntity>());
			}
		}

		public bool IsFileExist(string fileName, ProgApiResources apiRes)
		{
			Tuple<TransResult, List<FileInfoEntity>> progDetails = this.GetProgDetails("", apiRes);
			if (!progDetails.Item1.IsSuccess)
			{
				return false;
			}
			return progDetails.Item2.Any((FileInfoEntity x) => x.FullName == fileName);
		}

		public BaseFileRename FileRename()
		{
			return new BaseFileRename();
		}

		private int GetTimeout(HttpApiResources res)
		{
			return (res.RequestTimeout > 0) ? res.RequestTimeout : 10000;
		}
	}
}
